﻿using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace OCR
{
    /// <summary>
    /// 卡片管理类
    /// </summary>
    public class CardManager
    {
        private static List<Card> cards;
        /// <summary>
        /// 卡片列表
        /// </summary>
        public static List<Card> Cards
        {
            get
            {
                if (cards == null)
                {

                    cards = new List<Card>();
                }
                return cards;
            }
        }
        /// <summary>
        /// 导出excel
        /// </summary>
        /// <param name="imageFolder">图片文件夹</param>
        /// <param name="saveFilePath">导出文件路径</param>
        public static void ExportExcel(string imageFolder,string saveFilePath)
        {
            HSSFWorkbook workbook = new HSSFWorkbook();

            // 工作簿 
            ISheet sheet = workbook.CreateSheet("识别结果");
            // 工作表 
            IRow row = sheet.CreateRow(0);
            row.CreateCell(0).SetCellValue("运营商");
            row.CreateCell(1).SetCellValue("面值");
            row.CreateCell(2).SetCellValue("截止期");
            row.CreateCell(3).SetCellValue("卡号图片");
            row.CreateCell(4).SetCellValue("卡号");
            row.CreateCell(5).SetCellValue("密码图片");
            row.CreateCell(6).SetCellValue("密码");
            row.CreateCell(7).SetCellValue("图片文件名");

            int rowIndex = 1;
            foreach (Card card in cards)
            {
                IRow rowTmp = sheet.CreateRow(rowIndex);
                rowTmp.CreateCell(0).SetCellValue(card.Define.FactoryName);
                rowTmp.CreateCell(1).SetCellValue(card.Define.FaceValue);
                rowTmp.CreateCell(2).SetCellValue(card.Define.ValidDate.ToShortDateString());
                byte[] bytes = System.IO.File.ReadAllBytes(Path.Combine(imageFolder, card.NoImageName));
                int pictureIdx = workbook.AddPicture(bytes, PictureType.JPEG);
                HSSFPatriarch patriarch = (HSSFPatriarch)sheet.CreateDrawingPatriarch();
                // 插图片的位置  HSSFClientAnchor（dx1,dy1,dx2,dy2,col1,row1,col2,row2) 后面再作解释
                HSSFClientAnchor anchor = new HSSFClientAnchor(70, 10, 0, 0, 3, rowIndex, 4, rowIndex + 1);
                //把图片插到相应的位置
                HSSFPicture pict = (HSSFPicture)patriarch.CreatePicture(anchor, pictureIdx);

                rowTmp.CreateCell(4).SetCellValue(card.NoValue);
                byte[] bytes1 = System.IO.File.ReadAllBytes(Path.Combine(imageFolder, card.PwImageName));
                int pictureIdx1 = workbook.AddPicture(bytes1, PictureType.JPEG);
                HSSFPatriarch patriarch1 = (HSSFPatriarch)sheet.CreateDrawingPatriarch();
                // 插图片的位置  HSSFClientAnchor（dx1,dy1,dx2,dy2,col1,row1,col2,row2) 后面再作解释
                HSSFClientAnchor anchor1 = new HSSFClientAnchor(70, 10, 0, 0, 5, rowIndex, 6, rowIndex + 1);
                //把图片插到相应的位置
                HSSFPicture pict1 = (HSSFPicture)patriarch.CreatePicture(anchor1, pictureIdx1);

                rowTmp.CreateCell(6).SetCellValue(card.PwValue);
                rowTmp.CreateCell(7).SetCellValue(card.ImageName + ".jpg");
                rowIndex++;
            }
            using (Stream stream = File.OpenWrite(saveFilePath))
            {
                workbook.Write(stream);
            }
        }
    }
}
